﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Collections;
using System.Data;

namespace BusinessLogic
{

    public static class SelectAll
    {
        #region select hang hoa - chi tiet kho
        static SqlCommand cmd;
        static ArrayList arr;
        static SqlDataReader dr;
        static SqlConnection cn;
        public static Entities.ChiTietKhoHang[] SelectChiTietHangHoaTheoKho(Entities.ChiTietKhoHang chitiet)
        {
            Entities.ChiTietKhoHang[] arrC;
            try
            {
                string sql = "sp_Select_HangHoa";
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.HanhDong, SqlDbType.NVarChar, 50).Value = chitiet.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.MaKho, SqlDbType.VarChar, 50).Value = chitiet.MaKho;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietKhoHang row = new Entities.ChiTietKhoHang();
                    row.MaKho = (dr[Common.Constants.ChiTietKhoHang.MaKho].ToString());
                    row.TenKho = dr[Common.Constants.ChiTietKhoHang.TenKho].ToString();
                    row.MaHangHoa = (dr[Common.Constants.ChiTietKhoHang.MaHangHoa].ToString());
                    row.TenHangHoa = dr[Common.Constants.ChiTietKhoHang.TenHangHoa].ToString();
                    row.SoLuong = dr[Common.Constants.ChiTietKhoHang.SoLuong].ToString();
                    row.NgayNhap = DateTime.Parse(dr[Common.Constants.ChiTietKhoHang.NgayNhap].ToString()).Date;
                    row.HanSuDung = DateTime.Parse(dr[Common.Constants.ChiTietKhoHang.HanSuDung].ToString()).Date;
                    row.GhiChu = dr[Common.Constants.ChiTietKhoHang.GhiChu].ToString();
                    row.Deleted = bool.Parse(dr[Common.Constants.ChiTietKhoHang.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) return new Entities.ChiTietKhoHang[0];

                arrC = new Entities.ChiTietKhoHang[n];
                for (int i = 0; i < n; i++)
                {
                    arrC[i] = (Entities.ChiTietKhoHang)arr[i];
                }
                arr = null;
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                return arrC;
            }
            catch
            {
                return new Entities.ChiTietKhoHang[0];
            }
        }


        public static Entities.ChiTietKhoHang[] Select()
        {
            Entities.ChiTietKhoHang[] arrC;
            try
            {
                string sql = "Select * from ChiTietKhoHang";
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietKhoHang row = new Entities.ChiTietKhoHang();
                    row.MaKho = (dr[Common.Constants.ChiTietKhoHang.MaKho].ToString());
                    row.TenKho = dr[Common.Constants.ChiTietKhoHang.TenKho].ToString();
                    row.MaHangHoa = (dr[Common.Constants.ChiTietKhoHang.MaHangHoa].ToString());
                    row.TenHangHoa = dr[Common.Constants.ChiTietKhoHang.TenHangHoa].ToString();
                    row.SoLuong = dr[Common.Constants.ChiTietKhoHang.SoLuong].ToString();
                    row.NgayNhap = DateTime.Parse(dr[Common.Constants.ChiTietKhoHang.NgayNhap].ToString()).Date;
                    row.HanSuDung = DateTime.Parse(dr[Common.Constants.ChiTietKhoHang.HanSuDung].ToString()).Date;
                    row.GhiChu = dr[Common.Constants.ChiTietKhoHang.GhiChu].ToString();
                    row.Deleted = bool.Parse(dr[Common.Constants.ChiTietKhoHang.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) return new Entities.ChiTietKhoHang[0];

                arrC = new Entities.ChiTietKhoHang[n];
                for (int i = 0; i < n; i++)
                {
                    arrC[i] = (Entities.ChiTietKhoHang)arr[i];
                }
                arr = null;
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                return arrC;
            }
            catch
            {
                return new Entities.ChiTietKhoHang[0];
            }
        }

        public static Entities.HangHoa[] SelectHangHoaTheoKho(Entities.ChiTietKhoHang chitiet)
        {
            Entities.HangHoa[] list = null;
            try
            {
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand();
                cmd.CommandText = "sp_Select_HangHoa";
                cmd.Connection = cn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.HanhDong, SqlDbType.NVarChar, 50).Value = chitiet.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.MaKho, SqlDbType.VarChar, 50).Value = chitiet.MaKho;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.HangHoa row = new Entities.HangHoa();
                    row.HangHoaID = int.Parse(dr[Common.Constants.HangHoa.HangHoaID].ToString());
                    row.MaHangHoa = dr[Common.Constants.HangHoa.MaHangHoa].ToString();
                    row.TenHangHoa = dr[Common.Constants.HangHoa.TenHangHoa].ToString();
                    row.MaNhomHang = dr[Common.Constants.HangHoa.MaNhomHang].ToString();
                    row.TenNhomHang = dr[Common.Constants.HangHoa.TenNhomHang].ToString();
                    row.MaDonViTinh = dr[Common.Constants.HangHoa.MaDonViTinh].ToString();
                    row.GiaNhap = dr[Common.Constants.HangHoa.GiaNhap].ToString();
                    row.GiaBanBuon = dr[Common.Constants.HangHoa.GiaBanBuon].ToString();
                    row.GiaBanLe = dr[Common.Constants.HangHoa.GiaBanLe].ToString();
                    row.MaThue = dr[Common.Constants.HangHoa.MaThue].ToString();
                    row.GiaTriThue = dr[Common.Constants.HangHoa.GiaTriThue].ToString();
                    row.TenDonViTinh = dr["TenDonViTinh"].ToString();
                    row.MucDatHang = dr[Common.Constants.HangHoa.MucDatHang].ToString();
                    row.MucTonToiThieu = dr[Common.Constants.HangHoa.MucTonToiThieu].ToString();
                    row.LinkImage = dr[Common.Constants.HangHoa.LinkImage].ToString();
                    row.GhiChu = dr[Common.Constants.HangHoa.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.HangHoa.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) list = null;
                list = new Entities.HangHoa[n];
                for (int i = 0; i < n; i++)
                {
                    list[i] = (Entities.HangHoa)arr[i];
                }
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); list = null; }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }
            return list;
        }
        #endregion

        #region Thuế
        public static Entities.Thue[] Thue()
        {
            Entities.Thue t = new Entities.Thue();
            t.HanhDong = "1";
            Entities.Thue[] temp = BusinessLogic.Thue.Select(t);
            if (temp == null)
                temp = new Entities.Thue[0];
            return temp;
        }
        #endregion

        #region Tiền tệ
        public static Entities.TienTe[] TienTe()
        {
            Entities.TienTe t = new Entities.TienTe();
            t.HanhDong = "1";
            Entities.TienTe[] temp = BusinessLogic.TienTe.Select(t);
            if (temp == null)
                temp = new Entities.TienTe[0];
            return temp;
        }
        #endregion

        #region hàng hóa theo kho
        public static Entities.HangHoa[] HangHoaTheoKho(string makho)
        {
            Entities.ChiTietKhoHang kho = new Entities.ChiTietKhoHang();
            kho.HanhDong = "HangHoaTheoKho";
            kho.MaKho = makho;
            Entities.HangHoa[] hh = SelectHangHoaTheoKho(kho);
            return hh;
        }
        #endregion

        #region chi tiết kho hàng theo kho
        public static Entities.ChiTietKhoHang[] ChiTietKhoHangTheoKho(string makho)
        {
            Entities.ChiTietKhoHang tem = new Entities.ChiTietKhoHang();
            tem.MaKho = makho;
            tem.HanhDong = "ChiTietHangTheoKho";
            Entities.ChiTietKhoHang[] chitietkhohang;
            chitietkhohang = SelectChiTietHangHoaTheoKho(tem);
            return chitietkhohang;

        }
        #endregion

        #region quy đổi đơn vị tính
        //public static Entities.QuyDoiDonViTinh[] QuyDoiDonViTinh()
        //{
        //    Entities.QuyDoiDonViTinh[] temp = new QuyDoiDonViTinh().Select();
        //    if (temp == null)
        //        temp = new Entities.QuyDoiDonViTinh[0];
        //    return temp;
        //}
        #endregion

        #region Điểm thưởng khách hàng
        public static Entities.DiemThuongKhachHang[] DiemThuongKhachHang()
        {
            Entities.DiemThuongKhachHang[] temp = BusinessLogic.DiemThuongKhachHang.Select();
            if (temp == null)
                temp = new Entities.DiemThuongKhachHang[0];
            return temp;
        }
        public static Entities.TyLeTinh[] TyLeTinh()
        {
            Entities.TyLeTinh[] temp = BusinessLogic.TyLeTinh.Select();
            if (temp == null)
                temp = new Entities.TyLeTinh[0];
            return temp;
        }
        #endregion

        #region Gói hàng
        public static Entities.GoiHang[] GoiHang()
        {
            Entities.GoiHang t = new Entities.GoiHang();
            t.HanhDong = "1";
            Entities.GoiHang[] temp = BusinessLogic.GoiHang.Select(t);
            if (temp == null)
                temp = new Entities.GoiHang[0];
            return temp;
        }
        #endregion

        #region Chi tiết gói hàng
        public static Entities.ChiTietGoiHang[] ChiTietGoiHang()
        {
            Entities.ChiTietGoiHang t = new Entities.ChiTietGoiHang();
            t.HanhDong = "1";
            Entities.ChiTietGoiHang[] temp = BusinessLogic.ChiTietGoiHang.Select(t);
            if (temp == null)
                temp = new Entities.ChiTietGoiHang[0];
            return temp;
        }
        #endregion

        #region hóa đơn bán buôn
        public static Entities.HDBanBuon[] HDBanBuon()
        {
            Entities.HDBanBuon[] temp = BusinessLogic.HDBanBuon.Select();
            if (temp == null)
                temp = new Entities.HDBanBuon[0];
            return temp;
        }
        #endregion

        #region chi tiết hóa đơn bán buôn
        public static Entities.ChiTietHDBanBuon[] ChiTietHDBanBuon()
        {
            Entities.ChiTietHDBanBuon[] temp = BusinessLogic.ChiTietHDBanBuon.Select();
            if (temp == null)
                temp = new Entities.ChiTietHDBanBuon[0];
            return temp;
        }
        #endregion

        #region hóa đơn bán lẻ
        public static Entities.HDBanLe[] HDBanLe()
        {
            Entities.HDBanLe[] temp = BusinessLogic.HDBanLe.Select();
            if (temp == null)
                temp = new Entities.HDBanLe[0];
            return temp;
        }
        #endregion

        #region chi tiết hóa đơn bán lẻ
        public static Entities.ChiTietHDBanle[] ChiTietHDBanle()
        {
            Entities.ChiTietHDBanle[] temp = BusinessLogic.ChiTietHDBanLe.Select();
            if (temp == null)
                temp = new Entities.ChiTietHDBanle[0];
            return temp;
        }
        #endregion

        #region đơn vị tính
        public static Entities.DVT[] DVT()
        {
            Entities.DVT t = new Entities.DVT();
            t.HanhDong = "1";
            Entities.DVT[] temp = BusinessLogic.DVT.Select(t);
            if (temp == null)
                temp = new Entities.DVT[0];
            return temp;
        }
        #endregion

        #region nhân viên
        public static Entities.NhanVien[] NhanVien()
        {
            Entities.NhanVien nv = new Entities.NhanVien();
            nv.HanhDong = "0";
            nv.MaNhanVien = "";
            Entities.NhanVien[] temp = BusinessLogic.NhanVien.SelectNhanVien(nv);
            if (temp == null)
                temp = new Entities.NhanVien[0];
            return temp;
        }
        #endregion

        #region Nhóm hàng hóa
        public static Entities.NhomHangHoa[] NhomHang()
        {
            Entities.NhomHangHoa nh = new Entities.NhomHangHoa();
            nh.HanhDong = "1";
            Entities.NhomHangHoa[] temp = BusinessLogic.NhomHangHoa.Select(nh);
            if (temp == null)
                temp = new Entities.NhomHangHoa[0];
            return temp;
        }
        #endregion

        #region loại hàng hóa
        public static Entities.LoaiHang[] LoaiHang()
        {
            Entities.LoaiHang t = new Entities.LoaiHang();
            t.HanhDong = "1";
            Entities.LoaiHang[] temp = BusinessLogic.LoaiHang.SelectLoaiHang(t);
            if (temp == null)
                temp = new Entities.LoaiHang[0];
            return temp;
        }
        #endregion

        #region khách hàng
        public static Entities.KhachHang[] KhachHang()
        {
            Entities.KhachHang t = new Entities.KhachHang();
            t.HanhDong = "1";
            Entities.KhachHang[] tem = BusinessLogic.KhachHang.Select(t);
            if (tem == null)
                tem = new Entities.KhachHang[0];
            return tem;
        }
        #endregion

        public static Entities.SelectAll BanBuon(string makho)
        {
            Entities.SelectAll temp = new Entities.SelectAll();
            temp.Thue = Thue();
            temp.TienTe = TienTe();
            temp.HangHoa = HangHoaTheoKho(makho);
            temp.ChiTietKhoHang = ChiTietKhoHangTheoKho(makho);
            //temp.QuyDoiDonViTinh = QuyDoiDonViTinh();
            temp.DiemThuongKhachHang = DiemThuongKhachHang();
            temp.TyLeTinh = TyLeTinh();
            return temp;
        }

        public static Entities.SelectAll CongNo(string hangdong)
        {
            Entities.SelectAll temp = new Entities.SelectAll();
            if (hangdong == "KhachHang")
            {
                Entities.SelectTongHopCongNoKH sll = SelectTongHopCongNoKH.Select("CongNo");
                temp.HDBanBuon = sll.HoaDonBanBuon;
                temp.HDBanLe = sll.HoaDonBanLe;
                temp.KhachHang = sll.KhachHang;
                temp.PhieuTTCuaKH = sll.PhieuTTCuaKH;
                temp.ChiTietPhieuTTCuaKH = sll.ChiTietPhieuTTKH;               
            }
            else
            {
                Entities.SelectTongHopCongNo sl = SelectTongHopCongNo.Select("CongNo");
                temp.ChiTietPhieuTTNCC = sl.ChiTietPhieuTTNCC;
                temp.PhieuTTNCC = sl.PhieuTTNCC;
                temp.NhaCungCap = NhaCungCap.Select();
            }
            return temp;
        }

    }
}
#endregion